package com.PictureSite.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.PictureSite.controller.PictureSiteHomeController;
import com.PictureSite.model.PictureSitePostModel;

@SuppressWarnings("serial")
public class PictureSiteHomeServlet extends PictureSiteCommonServlet {
	
	private PictureSiteHomeController controller;
	
	private PictureSitePostModel model = new PictureSitePostModel();
	
	final private static String segmentView ="createPost.html";
	final private static String homeView ="home.html";
	final private static String errorView ="errorPage.html";
	
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
		this.controller = new PictureSiteHomeController(getSharedFileService(),getDataStore());
	}
	
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		model.setPageNum(controller.getPageNum(req));
		try {
			controller.filldata(model,req,resp);
			controller.bindModel(model, homeView, resp);
		} catch (Exception e) {
			try {
				controller.bindModel(model, errorView, resp);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
	}
	int count = 0;
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
			try {
				if (req.getParameter("id") != null) {
					controller.handleVotes(model,req);
				} else {
					controller.bindModel(model, segmentView, resp);
				}
			} catch (Exception e) {
		}
	}
}
